<?php

namespace app\admin\model;

use app\common\model\WlqTest;
use think\Model;
use app\common\library\Getui;
use think\Log;


class Notice extends Model
{

    

    

    // 表名
    protected $name = 'notice';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];

    public function getNeedConfirm()
    {
        return [
            1 => '是',
            2 => '否',
        ];
    }

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'get_user_id', [], 'LEFT')->setEagerlyType(0);
    }

    /**
     * 发送个推消息
     * @param array $noticeData Notice数据，包含 title, content, get_user_id
     * @return bool
     */
    public static function sendGetuiPush($noticeData)
    {
        if (empty($noticeData['get_user_id']) || empty($noticeData['title']) || empty($noticeData['content'])) {
            return false;
        }

        try {
            // 获取接收用户信息
            $user = User::where(['id' => $noticeData['get_user_id']])->find();
            if (!$user || empty($user['cid'])) {
                return false;
            }

            // 构建个推payload
            $payload = self::buildGetuiPayload($user['cid'], $noticeData['title'], $noticeData['content']);

            // 发送个推消息
            $getui = new Getui();
            $getui->pushToSingle($payload);

            return true;
        } catch (\Exception $e) {
            $add = [];
            $add['name'] = '个推消息';
            $add['content'] = $e->getMessage();
            WlqTest::create($add);
//            Log::write('Notice Getui push error: ' . $e->getMessage(), 'error');
            return false;
        }
    }

    /**
     * 构建个推payload
     * @param string $cid 客户端ID
     * @param string $title 标题
     * @param string $content 内容
     * @return array
     */
    protected static function buildGetuiPayload($cid, $title, $content)
    {
        $harmonyConfig = [
            'notification' => [
                'title' => $title,
                'body'  => $content,
                'click_type' => 'payload_custom',
                'category' => 'MARKETING',
                'payload' => 'pages/message/message',
            ],
        ];

        $androidConfig = [
            'ups' => [
                'notification' => [
                    'title' => $title,
                    'body'  => $content,
                    'click_type' => 'payload_custom',
                    'payload' => 'pages/message/message',
                ],
            ],
        ];

        // 构建推送参数
        return [
            'request_id' => uniqid(),
            'audience' => ['cid' => [$cid]],
            'settings' => [
                'ttl' => 7200000, // 消息有效期（毫秒），默认2小时
                'strategy' => [
                    'default' => 1,
                ],
            ],
            'push_message' => [
                'notification' => [
                    'title' => $title,
                    'body'  => $content,
                    'click_type' => 'payload_custom',
                    'payload' => 'pages/message/message',
                ]
            ],
            'push_channel' => [
                'android' => $androidConfig,
                'harmony' => $harmonyConfig,
            ],
        ];
    }




}
